# -*- coding: utf-8 -*-

'''eof
name:申请企业当前未到期相关还款责任五级分类为关注类余额
code:EC89_EntCurrRelaGuaranteeConsAccBalance
tableName:
columnName:
groups:行内二代企业征信数据
dependencies:EC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''

def getTable(data, tableName, occurTimes, iteratorTimes):
    '''
    获取表信息
    :param data:表上层的字典对象
    :param tableName:表的名字
    :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
    :param iteratorTimes:下级字段出现的次数，如果是0代表下级字段可以不出现，如果是1代表下级字段必须出现
    :return:该表全部数据  返回[{}]为异常缺失，返回0为正常缺失，其他返回正常数组
    '''
    try:
        table = data.get(tableName)
        if occurTimes == 0:
            if table is None or (table == "" and iteratorTimes == 0):
                return 0
            elif isinstance(table, list) is False or len(table) == 0 or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
        if occurTimes == 1:
            if table == "" and iteratorTimes == 0:
                return 0
            if table is None or isinstance(table, list) is False or len(table) == 0 \
                    or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
    except:
        return [{}]

def isfloat(x):
    '''
    判断x为float类型且大于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x) >= 0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def EC89_EntCurrRelaGuaranteeConsAccBalance():
    data = EC_DSJ_2D.get('data')
    #相关还款责任汇总信息,出现次数[1..1],下级字段出现次数[0..1]
    EBE = getTable(data, 'EBE', 1, 0)
    if EBE == [{}]:
        return u'缺失值'
    elif EBE == 0:
        return 0
    else:
        #相关还款责任汇总信息单元,出现次数[0..1]，下级字段出现次数[0..1]
        EB05 = getTable(EBE[0], 'EB05', 0, 0)
        if EB05 == [{}]:
            return u"缺失值"
        elif EB05 == 0:
            return 0
        else:
            #借贷交易相关还款责任汇总信息段，出现次数[0..1]，下级字段出现次数[0..4]
            EB05A = getTable(EB05[0], 'EB05A', 0, 0)
            #担保交易相关还款责任汇总信息段，出现次数[0..1]，下级字段出现次数[0..4]
            EB05B = getTable(EB05[0], 'EB05B', 0, 0)
            if EB05A == [{}] and EB05B == [{}]:
                return u"缺失值"
            elif EB05A == 0 and EB05B == 0:
                return 0
            else:
                #借贷交易相关还款责任汇总信息,出现次数[0..4],下级字段出现次数[1..1]
                EB05AH = getTable(EB05A[0], 'EB05AH', 0, 1)
                #担保交易相关还款责任汇总信息,出现次数[0..4]，下级字段出现次数[1..1]
                EB05BH = getTable(EB05B[0], 'EB05BH', 0, 1)
                if EB05AH == [{}] and EB05BH == [{}]:
                    return u"缺失值"
                elif EB05AH == 0 and EB05BH == 0:
                    return 0
                else:
                    conts = 0
                    balance = 0
                    for i in EB05AH:
                        try:
                            #责任类型
                            EB05AD01 = i.get('EB05AD01')
                            #其他借贷交易账户关注类余额
                            EB05AJ05 = i.get('EB05AJ05')
                            if EB05AD01 in null_type_list or EB05AJ05 in null_type_list:
                                conts += 1
                            else:
                                if EB05AD01 in ["0",0]:
                                    if isfloat(EB05AJ05):
                                        balance += float(EB05AJ05)
                                    else:
                                        pass
                                else:
                                    pass
                        except:
                            conts += 1
                    for i in EB05BH:
                        try:
                            #责任类型
                            EB05BD01 = i.get('EB05BD01')
                            #关注类余额
                            EB05BJ03 = i.get('EB05BJ03')
                            if EB05BD01 in null_type_list or EB05BJ03 in null_type_list:
                                conts += 1
                            else:
                                if EB05BD01 in ["0",0]:
                                    if isfloat(EB05BJ03):
                                        balance += float(EB05BJ03)
                                    else:
                                        pass
                                else:
                                    pass
                        except:
                            conts += 1
                            
                    if conts == len(EB05AH) + len(EB05BH):
                        return u"缺失值"
                    else:
                        return round(balance,4)

result = EC89_EntCurrRelaGuaranteeConsAccBalance()
